$(function () {

    $("#shop-title").html(" <div class=\"top-con w1200\">\n" +
        "        <p class=\"t-con-l f-l\" id = \"userInfo\"></p>\n" +
        "        <ul class=\"t-con-r f-r\">\n" +
        "            <li><a href=\"#\">我 (个人中心)</a></li>\n" +
        "            <li><a id='my_car' onclick='getCarList()' href=\"javascript:void(0)\">我的购物车</a></li>\n" +
        "            <li><a href=\"javascript:void(0)\" onclick='getUserCarList()'>我的订单</a></li>\n" +
        "            <li class=\"erweima\">\n" +
        "                <a href=\"#\">扫描二维码</a>\n" +
        "                <div class=\"ewm-tu\">\n" +
        "                    <a href=\"#\"><img src=\"images/erweima-tu.jpg\" /></a>\n" +
        "                </div>\n" +
        "            </li>\n" +
        "            <div style=\"clear:both;\"></div>\n" +
        "        </ul>\n" +
        "        <div style=\"clear:both;\"></div>\n" +
        "    </div>");


    // 1.发送一个请求携带token过去，然后获取用户信息
    sendRequestCallbackToken("shop-sso/ssoController/getUserInfoByToken", null, function (data) {
        if (data.status == "success") {
            $("#userInfo").html("欢迎【" + data.data + "】登录，<a href='javascript:void(0)' onclick='logout()'>注销</a>");
        } else {
            $("#userInfo").html("您还没有登录，请先<a onclick='toLoginPage()' href='javascript:void(0)'>登录</a>");
        }
    });

    // 2.查询用户购物车的数量
    sendRequestCallbackToken("shop-car/carController/getUserCarCount", null, function (data) {
        if (data.status == "success") {
            $("#my_car").html("我的购物车<span style='color: red'>("+data.data+")</span>");
        }
    });

})

function getCarList() {

    // 1.获取token
    var token = localStorage.getItem("2101-login-token")

    // 2.判断是否有token
    if (token) {
        location.href = "http://localhost/shop-car/carController/getCarList?token=" + token;
    } else {
        location.href = "http://localhost/shop-car/carController/getCarList";
    }

}

function logout() {
    localStorage.removeItem("2101-login-token");
    location.href = "http://localhost/shop-home";
}

function toLoginPage() {

    // 1.获取当前地址
    var url = location.href;

    // 2.再调整到登录页面
    location.href = "http://localhost/shop-sso/toLoginPage?returnUrl=" + url;
}

function getUserCarList(){
    var token = localStorage.getItem("2101-login-token")
    if (token) {
        location.href = "http://localhost/shop-order/orderController/getUserOrderList?token=" + token;
    } else {
        location.href = "http://localhost/shop-order/orderController/getUserOrderList";
    }
}